package net.neuronal.tasador;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

public class Util {

	public static Map<String,String> readFile(String fileName, int count){
		
		Map<String,String> map = new HashMap<String, String>();
		
		BufferedReader br = null;
		InputStream in = null;
		
		try {
			in = Util.class.getResourceAsStream(fileName);
		     
			br = new BufferedReader(new InputStreamReader(in));
	
			String line;
			
			int i = 1;
			
			while ((line = br.readLine()) != null) {
				map.put(line, normaliza(i, 1, count));
				i++;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			try {
				in.close();
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return map;
	}
	
	
	public static Map<String,String> readFile(String fileName, int min, int max){
		
		Map<String,String> map = new HashMap<String, String>();
		
		BufferedReader br = null;
		
		try {
			
			br = new BufferedReader(new FileReader(new File(fileName)));
			
			String line;
			
			while ((line = br.readLine()) != null) {
				map.put(line, normaliza(Integer.parseInt(line), min, max));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return map;
	}
	
	public static String normaliza(int x, int min, int max){		
		return new BigDecimal(x - min).divide(new BigDecimal(max - min), 3, RoundingMode.CEILING).toPlainString();
	}
	
	public static String desnormaliza(BigDecimal x, int min, int max){		
		return x.multiply(new BigDecimal(max - min)).add(new BigDecimal(min)).toPlainString();
	}
}
